package com.hyg.module_report.ui.customView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.hyg.module_report.R;


public class WuXingView extends View {

    //宽度
    private int mWidth;
    //高度
    private int mHeight;

    private double xin = 90;
    private double fei = 80;
    private double gan = 70;
    private double pi  = 60;
    private double shen= 50;

    private String Sex = "女";

    //背景圆心坐标
    private float BGcenterX;
    private float BGcenterY;

    private Context context;


    public WuXingView(Context context) {
        super(context);
        this.context = context;
    }

    public WuXingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        this.context = context;
    }

    public WuXingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        this.context = context;
    }

    public WuXingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.context = context;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawSex(canvas);
    }

    private void drawSex(Canvas canvas){
        BGcenterX = getWidth() / 2;
        BGcenterY = getHeight() / 2;

        int mBitWidth = 0;
        int mBitHeight = 0;

        if (Sex.equals("男")|| Sex.equals("先生")) {
            Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(R.mipmap.wuxing_male)).getBitmap();
            mBitWidth = bitmap.getWidth();
            mBitHeight = bitmap.getHeight();
//            setImgSize(bitmap,mBitWidth,mBitHeight);
            canvas.drawBitmap(bitmap,  BGcenterX - mBitWidth / 2 ,  BGcenterY - mBitHeight/2, null);
        } else {
            Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(R.mipmap.wuxing_female)).getBitmap();
            mBitWidth = bitmap.getWidth();
            mBitHeight = bitmap.getHeight();
            canvas.drawBitmap(bitmap,  BGcenterX - mBitWidth / 2 ,  BGcenterY - mBitHeight / 2, null);
        }

        Bitmap bp = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_all)).getBitmap();
        int xingBitWidth = bp.getWidth();
        int xingBitHeight = bp.getHeight();

        float showX_gan = BGcenterX;
        float showY_gan =  ( BGcenterY - mBitHeight/2 ) - xingBitHeight * 2;
        setXingXing(gan,mBitWidth,mBitHeight,showX_gan,showY_gan,canvas);

//        float showX_xin = BGcenterX + mBitWidth / 2 + xingBitWidth * 2.5f;
//        float showY_xin =  BGcenterY - xingBitHeight * 2 ;
//        setXingXing(xin,mBitWidth,mBitHeight,showX_xin,showY_xin,canvas);
//        float showX_shen = BGcenterX - mBitWidth / 2 - xingBitWidth * 2.5f;
//        float showY_shen = BGcenterY - xingBitHeight * 2 ;
//        setXingXing(shen,mBitWidth,mBitHeight,showX_shen,showY_shen,canvas);
//        float showX_fei = BGcenterX - mBitWidth / 2 - xingBitWidth * 0.5f;
//        float showY_fei = BGcenterY + mBitHeight / 2 - xingBitHeight * 5;
//        setXingXing(fei,mBitWidth,mBitHeight,showX_fei,showY_fei,canvas);
//        float showX_pi = BGcenterX + mBitWidth / 2 + xingBitWidth * 0.5f;
//        float showY_pi =  BGcenterY + mBitHeight / 2 - xingBitHeight * 5;
//        setXingXing(pi,mBitWidth,mBitHeight,showX_pi,showY_pi,canvas);

        float showX_xin = BGcenterX + mBitWidth / 2 - xingBitWidth *2f;
        float showY_xin =  BGcenterY - xingBitHeight * 5.5f ;
        setXingXing(xin,mBitWidth,mBitHeight,showX_xin,showY_xin,canvas);

        float showX_shen = BGcenterX - mBitWidth / 2 + xingBitWidth * 2f;
        float showY_shen = BGcenterY - xingBitHeight * 5.5f ;
        setXingXing(shen,mBitWidth,mBitHeight,showX_shen,showY_shen,canvas);

        float showX_fei = BGcenterX - mBitWidth / 2 + xingBitWidth * 4.5f;
        float showY_fei = BGcenterY + mBitHeight / 2 - xingBitHeight * 3;
        setXingXing(fei,mBitWidth,mBitHeight,showX_fei,showY_fei,canvas);

        float showX_pi = BGcenterX + mBitWidth / 2 - xingBitWidth * 4.5f;
        float showY_pi =  BGcenterY + mBitHeight / 2 - xingBitHeight * 3;
        setXingXing(pi,mBitWidth,mBitHeight,showX_pi,showY_pi,canvas);

        canvas.save();
//        canvas.restore();//别忘了restore
    }

    private void setXingXing(double xingScore, int mBitWidth, int mBitHeight, float showX, float showY, Canvas canvas){
        Bitmap bitmap1 = null;
        Bitmap bitmap2 = null;
        Bitmap bitmap3 = null;
        Bitmap bitmap4 = null;
        Bitmap bitmap5 = null;
        if(xingScore >= 60){
            bitmap1 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_all)).getBitmap();
            bitmap2 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_all)).getBitmap();
            bitmap3 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_all)).getBitmap();
            if(xingScore >= 100) {
                bitmap4 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_all)).getBitmap();
                bitmap5 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_all)).getBitmap();
            }else if(xingScore >= 90) {
                bitmap4 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_all)).getBitmap();
                bitmap5 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_half)).getBitmap();
            }else if(xingScore >= 80){
                bitmap4 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_all)).getBitmap();
                bitmap5 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_empty)).getBitmap();
            }else if(xingScore >= 70) {
                bitmap4 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_half)).getBitmap();
                bitmap5 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_empty)).getBitmap();
            }else{
                bitmap4 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_empty)).getBitmap();
                bitmap5 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_empty)).getBitmap();
            }
        }else if(xingScore >= 50) {
            bitmap1 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_all)).getBitmap();
            bitmap2 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_all)).getBitmap();
            bitmap3 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_half)).getBitmap();
            bitmap4 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_empty)).getBitmap();
            bitmap5 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_empty)).getBitmap();
        }else{
            bitmap3 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_empty)).getBitmap();
            bitmap4 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_empty)).getBitmap();
            bitmap5 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_empty)).getBitmap();
            if(xingScore >= 40) {
                bitmap1 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_all)).getBitmap();
                bitmap2 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_all)).getBitmap();
            }else if(xingScore >= 30) {
                bitmap1 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_all)).getBitmap();
                bitmap2 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_half)).getBitmap();
            }else if(xingScore >= 20){
                bitmap1 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_all)).getBitmap();
                bitmap2 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_empty)).getBitmap();
            }else if(xingScore >= 10) {
                bitmap1 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_half)).getBitmap();
                bitmap2 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_empty)).getBitmap();
            }else{
                bitmap1 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_empty)).getBitmap();
                bitmap2 = ((BitmapDrawable) getResources().getDrawable(R.mipmap.xing_empty)).getBitmap();
            }
        }
        int xingBitWidth = bitmap1.getWidth();
//        int xingBitHeight = bitmap1.getHeight();
//                    setImgSize(bitmap,mBitWidth,(int)BGcenterX * 35 / 10);
        canvas.drawBitmap(bitmap1,  showX - (xingBitWidth * 5/ 2 ),  showY, null);
        canvas.drawBitmap(bitmap2,  showX - (xingBitWidth * 3/2 ),  showY, null);
        canvas.drawBitmap(bitmap3,  showX - xingBitWidth / 2,  showY, null);
        canvas.drawBitmap(bitmap4,  showX + (xingBitWidth/2 ),  showY, null);
        canvas.drawBitmap(bitmap5,  showX + (xingBitWidth * 3 / 2 ),  showY, null);
        canvas.save();
    }


    public Bitmap setImgSize(Bitmap bm, int newWidth, int newHeight) { // 获得图片的宽高.
        int width = bm.getWidth();
        int height = bm.getHeight(); // 计算缩放比例.
        float scaleWidth = ((float) 0.8*newWidth) / width;
        float scaleHeight = ((float)0.8*newHeight) / height; // 取得想要缩放的matrix参数.
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight); // 得到新的图片.
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        return newbm;
    }

    //设置数值
    public void setData(double xin, double fei, double gan, double pi, double shen, String sex) {
        this.xin = xin;
        this.fei = fei;
        this.gan = gan;
        this.pi = pi;
        this.shen = shen;
        this.Sex = sex;

        postInvalidate();
    }



}
